import {defineStore} from "pinia";
import {ref} from "vue";
import type {GameObj} from "../utils/types/Game";
import {GameInfoByName} from "../reqApi/Game";
import type {ResultInfo} from "../utils/types/ResultInfo";

// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 store 的唯一 id
export const StoreSearchKeyWord = defineStore('SearchKeyWord', () => {
    const keyWord = ref("")
    const SetKeyWord = (info: string) => {
        keyWord.value = info
    }
    return {keyWord, SetKeyWord}

})
export const StoreGameInfoSearch = defineStore('StoreGameInfoSearch', () => {
    const resultGames = ref<GameObj[]>([])

    const Get = (): GameObj[] => resultGames.value
    const Flush = () => {
        resultGames.value.length = 0
        GameInfoByName({name: StoreSearchKeyWord().keyWord}).then(resp => {
            const result = resp.data as ResultInfo<GameObj[]>
            if (result.success) {
                resultGames.value = result.data || []
            }
        })
    }
    return {resultGames, Get, Flush}
})
